package com.anlogic.sdk.bootimage.internal.ui.composite;

import com.anlogic.sdk.bootimage.internal.ui.PartitionConfigDialog;
import com.anlogic.sdk.bootimage.internal.ui.PartitionLabelProvider;
import com.anlogic.sdk.bootimage.model.BootGenModel;
import com.anlogic.sdk.bootimage.model.Partition;
import com.anlogic.sdk.bootimage.model.options.PartionType;
import java.util.List;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class PartitionTableComposite extends AbsBootgenComposite {
	private TableViewer tableViewer;

	private Table table;

	private Button btnAdd;

	private Button btnDelete;

	private Button btnUp;

	private Button btnEdit;

	private Button btnDown;

	public PartitionTableComposite(Composite parent, BootGenModel model) {
		super(parent, 0, model);
		this.model = model;
		GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(this);
		createUI(this);
	}

	private void createUI(Composite container) {
		Label lblPartitions = new Label(container, 0);
		GridDataFactory.swtDefaults().span(2, 1).applyTo((Control) lblPartitions);
		lblPartitions.setText("Boot image partitions");
		this.tableViewer = new TableViewer(container, 67584);
		this.table = this.tableViewer.getTable();
		this.table.setHeaderVisible(true);
		GridDataFactory.fillDefaults().grab(true, true).applyTo((Control) this.table);
		TableViewerColumn col_1 = new TableViewerColumn(this.tableViewer, 0);
		TableColumn filePathColumn = col_1.getColumn();
		filePathColumn.setWidth(350);
		filePathColumn.setText("File path");
		col_1.setLabelProvider((CellLabelProvider) new PartitionLabelProvider(0));
		TableViewerColumn col_2 = new TableViewerColumn(this.tableViewer, 0);
		TableColumn encryptedColumn = col_2.getColumn();
		encryptedColumn.setWidth(90);
		encryptedColumn.setText("Encrypted");
		col_2.setLabelProvider((CellLabelProvider) new PartitionLabelProvider(1));
		TableViewerColumn col_3 = new TableViewerColumn(this.tableViewer, 0);
		TableColumn authenticatedColumn = col_3.getColumn();
		authenticatedColumn.setText("Authenticated");
		authenticatedColumn.setWidth(90);
		col_3.setLabelProvider((CellLabelProvider) new PartitionLabelProvider(2));
		Composite buttonContainer = new Composite(container, 0);
		GridDataFactory.swtDefaults().applyTo((Control) buttonContainer);
		GridLayoutFactory.fillDefaults().applyTo(buttonContainer);
		this.btnAdd = new Button(buttonContainer, 0);
		this.btnAdd.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		this.btnAdd.setText("Add");
		this.btnDelete = new Button(buttonContainer, 0);
		this.btnDelete.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		this.btnDelete.setText("Delete");
		this.btnEdit = new Button(buttonContainer, 0);
		this.btnEdit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		this.btnEdit.setText("Edit");
		this.btnUp = new Button(buttonContainer, 0);
		this.btnUp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		this.btnUp.setText("Up");
		this.btnDown = new Button(buttonContainer, 0);
		this.btnDown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		this.btnDown.setText("Down");
		this.tableViewer.setContentProvider((IContentProvider) new ArrayContentProvider());
	}

	private void enableUpDownControls(int idx) {
		if (!this.model.isNew())
			return;
		if (idx == 0 && this.table.getItemCount() == 1) {
			this.btnUp.setEnabled(false);
			this.btnDown.setEnabled(false);
		} else if (idx == 0) {
			this.btnUp.setEnabled(false);
			this.btnDown.setEnabled(true);
		} else if (idx == this.table.getItemCount() - 1) {
			this.btnUp.setEnabled(true);
			this.btnDown.setEnabled(false);
		} else {
			this.btnUp.setEnabled(true);
			this.btnDown.setEnabled(true);
		}
		int selectionIndex = this.table.getSelectionIndex();
		if (selectionIndex >= 0) {
			Partition partition = (Partition) this.table.getItem(selectionIndex).getData();
			if (partition.getPartitionType() == PartionType.BOOTLOADER)
				this.btnDown.setEnabled(false);
		}
		if (selectionIndex >= 1) {
			Partition partition = (Partition) this.table.getItem(selectionIndex - 1).getData();
			if (partition.getPartitionType() == PartionType.BOOTLOADER)
				this.btnUp.setEnabled(false);
		}
	}

	public void loadUI() {
		this.tableViewer.setInput(this.model.getPartitions());
	}

	public void refreshTable() {
		this.tableViewer.refresh();
	}

	public Object getSelectedElement() {
		ISelection selection = this.tableViewer.getSelection();
		if (!(selection instanceof StructuredSelection))
			return null;
		StructuredSelection structuredSelection = (StructuredSelection) selection;
		Object element = structuredSelection.getFirstElement();
		return element;
	}

	private void selectPartitionId(int i) {
		this.table.select(i);
		enableUpDownControls(i);
	}

	public void addListeners() {
		this.table.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				TableItem tableItem = (TableItem) e.item;
				if (tableItem != null) {
					int idx = PartitionTableComposite.this.table.indexOf(tableItem);
					if (tableItem != null)
						PartitionTableComposite.this.enableUpDownControls(idx);
				}
			}
		});
		this.btnAdd.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PartitionTableComposite.this.addParition();
			}
		});
		this.btnDelete.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PartitionTableComposite.this.deletePartition();
			}
		});
		this.btnEdit.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PartitionTableComposite.this.editSelectedPartition();
			}
		});
		this.btnDown.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PartitionTableComposite.this.movePartitionDown();
			}
		});
		this.btnUp.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				PartitionTableComposite.this.movePartitionUp();
			}
		});
		this.tableViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				PartitionTableComposite.this.editSelectedPartition();
			}
		});
	}

	private void addParition() {
		Partition partition = Partition.newPartition(this.model.getChipType());
		if (this.model.getPartitions().size() == 0)
			partition.setPartitionType(PartionType.BOOTLOADER);
		PartitionConfigDialog addPartitionDlg = getPartitionDialog(partition);
		int open = addPartitionDlg.open();
		if (open == 0) {
			this.model.addPartition(partition);
			runPostModifyOperations();
		}
	}

	protected PartitionConfigDialog getPartitionDialog(Partition partition) {
		PartitionConfigDialog addPartitionDlg = new PartitionConfigDialog(Display.getCurrent().getActiveShell(),
				partition, this.model);
		return addPartitionDlg;
	}

	private void runPostModifyOperations() {
		refreshTable();
		markEditedAndValidate();
	}

	private void deletePartition() {
		Object selectedElement = getSelectedElement();
		if (selectedElement != null && selectedElement instanceof Partition) {
			this.model.getPartitions().remove(selectedElement);
			runPostModifyOperations();
		}
	}

	private void editSelectedPartition() {
		Object selectedElement = getSelectedElement();
		if (selectedElement != null && selectedElement instanceof Partition) {
			Partition selectedPartition = (Partition) selectedElement;
			Partition editablePartition = selectedPartition.getCopy();
			PartitionConfigDialog addPartDialogue = getPartitionDialog(editablePartition);
			addPartDialogue.setEditMode(true);
			int open = addPartDialogue.open();
			if (open == 0) {
				selectedPartition.editPartition(editablePartition);
				runPostModifyOperations();
			}
		}
	}

	private void movePartitionDown() {
		Object selectedElement = getSelectedElement();
		if (selectedElement == null)
			return;
		List<Partition> partitions = this.model.getPartitions();
		int index = partitions.indexOf(selectedElement);
		if (index == partitions.size() - 1)
			return;
		partitions.remove(index);
		partitions.add(index + 1, (Partition) selectedElement);
		refreshTable();
		selectPartitionId(index + 1);
		markEditedAndValidate();
	}

	private void movePartitionUp() {
		Object selectedElement = getSelectedElement();
		if (selectedElement == null)
			return;
		List<Partition> partitions = this.model.getPartitions();
		int index = partitions.indexOf(selectedElement);
		if (index == 0)
			return;
		partitions.remove(index);
		partitions.add(index - 1, (Partition) selectedElement);
		refreshTable();
		selectPartitionId(index - 1);
		markEditedAndValidate();
	}
}
